<?php
/*
	根据行政区划获取下级代理
	先整理用户所有代理区域代码
*/
defined('BY_JYA') or exit('error');
list($mid,$self)=$arguments;
$agents=$this->getall('agent',array('mid'=>$mid),array('type','area_code'));
$codes=array();
foreach($agents as $agent){
	if($self){
		$codes[]=$agent['area_code'];
	}
	if($agent['type']==1){
		//省级
		$c_codes=pdo_getall('core_area',array('parent_code'=>$agent['area_code']),array('code'),'code');
		if($c_codes){
			$codes=array_merge(array_keys($c_codes),$codes);
			foreach($c_codes as $c){
				$d_codes=pdo_getall('core_area',array('parent_code'=>$c['code']),array('code'),'code');
				if($d_codes){
					$codes=array_merge(array_keys($d_codes),$codes);
				}
			}
		}
	}
	if($agent['type']==2){
		//市级
		$d_codes=pdo_getall('core_area',array('parent_code'=>$agent['area_code']),array('code'),'code');
		if($d_codes){
			$codes=array_merge(array_keys($d_codes),$codes);
		}
	}
}
if($codes){
	$ids=$this->getall('agent',array('area_code'=>$codes),array('id'),'id');
}
if($ids){
	return array_keys($ids);
}else{
	return 0;
}



/*
$ids=array($mid);
$sun_ids=$this->getall('agent',array('agent_id'=>$mid),array('mid'),'mid');
$sun_ids=array_keys($sun_ids);
if($sun_ids){
	foreach($sun_ids as $id){
		$row=$this->get_agent_team($id);
		$ids=array_merge($row,$ids);
	}
}*/
return $ids;
